package com.anan.builder;

import com.anan.bean.TableInfo;
import com.anan.utils.StringTools;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BuildService {

    public static void create(TableInfo tableInfo,String projectName){
        createService(tableInfo, projectName);
        createServiceImp(tableInfo, projectName);
    }


    public static void createBaseService(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/service";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "BaseService.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.service;\n" +
                    "\n" +
                    "import org.apache.ibatis.annotations.Param;\n" +
                    "\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    "public interface BaseService<T,V> {\n" +
                    "    Integer insert(V query);\n" +
                    "    Integer delete(T query);\n" +
                    "    Integer update(T query);\n" +
                    "    V get(T query);\n" +
                    "    V getByFuzzyQuery(T query);\n" +
                    "    List<V> list(T query);\n" +
                    "    List<V> list();\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createService(TableInfo tableInfo,String projectName) {
        String beanName = tableInfo.getBeanName();
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/service";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName+"Service.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.service;\n" +
                    "\n" +
                    "import com.anan.entity.po."+beanName+";\n" +
                    "import com.anan.entity.query."+beanName+"Query;\n" +
                    "\n" +
                    "public interface "+beanName+"Service extends BaseService<"+beanName+"Query,"+beanName+">{\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createServiceImp(TableInfo tableInfo,String projectName) {

        String beanName = tableInfo.getBeanName();
        String beanNameB = StringTools.lowerCaseFirstLetter(beanName);


        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/service/imp";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName+"ServiceImp.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.service.imp;\n" +
                    "\n" +
                    "import com.anan.entity.po."+beanName+";\n" +
                    "import com.anan.entity.query."+beanName+"Query;\n" +
                    "import com.anan.exception.BusinessException;\n" +
                    "import com.anan.mapper."+beanName+"Mapper;\n" +
                    "import com.anan.service."+beanName+"Service;\n" +
                    "import org.springframework.stereotype.Service;\n" +
                    "\n" +
                    "import javax.annotation.Resource;\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    "@Service\n" +
                    "public class "+beanName+"ServiceImp implements "+beanName+"Service {\n" +
                    "\n" +
                    "    @Resource\n" +
                    "    "+beanName+"Mapper "+beanNameB+"Mapper;\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public Integer insert("+beanName+" query) {\n" +
                    "        return "+beanNameB+"Mapper.insert(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public Integer delete("+beanName+"Query query) {\n" +
                    "        return "+beanNameB+"Mapper.delete(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public Integer update("+beanName+"Query query) {\n" +
                    "        return "+beanNameB+"Mapper.update(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public "+beanName+" get("+beanName+"Query query) {\n" +
                    "        return "+beanNameB+"Mapper.get(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public "+beanName+" getByFuzzyQuery("+beanName+"Query query) {\n" +
                    "        return "+beanNameB+"Mapper.getByFuzzyQuery(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public List<"+beanName+"> list("+beanName+"Query query) {\n" +
                    "        return "+beanNameB+"Mapper.list(query);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public List<"+beanName+"> list() {\n" +
                    "        return "+beanNameB+"Mapper.list();\n" +
                    "    }\n" +
                    "\n" +

                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
